﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Brain.Editor;
using Brain.SunBurn;
using SynapseGaming.LightingSystem.Rendering;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using SynapseGaming.LightingSystem.Lights;
using Brain.Build;

namespace Brain.Plugins.SunBurn
{
    public class SunBurnPluginDeferred : IEditorPlugin
    {
        public void Load()
        {
            SplashScreenGameComponent comp = new SplashScreenGameComponent(Engine.Instance.Game);
            Engine.Instance.Game.Components.Add(comp);

            Engine.Instance.AddManager(new SunBurnRenderManager3D(Engine.Instance.GraphicsDevice, RenderingSystemType.Deferred));

            EndReload();
        }


        public void Reload()
        {
            Engine.Instance.AddManager(new SunBurnRenderManager3D(Engine.Instance.GraphicsDevice, RenderingSystemType.Deferred));

            // Reload is called 'usually' when the Engine is loading a Scene from a file.
            // This is the very first method to be called, so we don't have any GameScreens active.
            // The Camera we did on the Load method must wait to be done on the EndReload()
        }

        public void EndReload()
        {
            GameScreen s = Engine.Instance.ScreenManager.GetScreen(0);
            InputFreeCamera3D free = new InputFreeCamera3D(s);
            free.ShouldSerialize = false;

            free.FarPlane = 10000;
            free.NearPlane = 1;
            PresentationParameters pp = Engine.Instance.GraphicsDevice.PresentationParameters;
            free.AspectRatio = pp.BackBufferWidth / (float)pp.BackBufferHeight;

            Engine.Instance.CameraManager.Camera3D = free;

            ContentFileUtil.CustomExtensions.Add("fbx", "DeferredLightingSystemModelProcessor");
            ContentFileUtil.CustomExtensions.Add("x", "DeferredLightingSystemModelProcessor");
            ContentBuilder.PipelineAssemblies.Add("SynapseGaming-SunBurn-Processors-Pro.dll");

            IEditorForm form = Engine.Instance.Editor;
            form.AddFactoryType(typeof(PointLight),
                delegate(object o)
                {
                    PointLight light = o as PointLight;
                    Engine.Instance.RenderManager3D.SubmitLight(light);
                }, "Point Light", "SunBurn");
        }        
    }
}
